package ConllctionNofair;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 *  set 不安全
 */
public class setTest {
    public static void main(String[] args) {
        //  HashSet
//        1. 使用集合类
//        HashSet<String> set = new HashSet<>();
//        Set<String> set1 = Collections.synchronizedSet(set);
//        2. 使用 copyOnwriter
        CopyOnWriteArraySet<String> set = new CopyOnWriteArraySet<>();
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                set.add(UUID.randomUUID().toString().substring(0,5));
                System.out.println(set);
            },i+"").start();
        }

    }
}
